//
//  DishPhotoView.m
//  XRmenu
//
//  Created by Chow Akins on 11-3-20.
//  Copyright 2011 E0571. All rights reserved.
//

#import "DishPhotoView.h"
#import "DishVideoView.h"

#import "DishTermialView.h"



#define kTagOfBigPhoto 130



#define kTagOfVideoView 331
#define kTagOfDetailView 332
#define kTagOfTerimalView 333
#define kTagOfRelateView 334

#define kTagOfDishControl 340

#define kTagOperView 361


@implementation DishPhotoView
@class DishControl;
@class DishRelateView;
@synthesize bgMusic;
@synthesize curProduct;
-(void)buttonClicked:(id)sender
{
	NSInteger tag=[sender tag];
	switch (tag) {
		case kTagOfStartBT:
			DebugLog(@"开启工具栏");
			postN(kMessage_TopBar_The3rd_Controller);
			break;
		default:
			break;
	}
	
}
-(id) initWithStyle:(NSString *)stylename DishSubViewName:(NSString *)vname DishId:(NSString *)dishid
{
	self = [super initWithStyle:stylename DishSubViewName:vname];
	if (self) {
		addNObserver(@selector(changeRelateDish:),kMessage_Dish_changeRelateDish);
		addNObserver(@selector(refreshControlStatus),kMessage_Photo_orderedChange);
		
		[self add1fingerLeftSwipe];
		[self add1fingerRightSwipe];
		[self add1fingerTopSwipe];
		[self add1fingerDownSwipe];
		[self add1fingerTap];
		[self addPinch];
		
		curProduct=nil;
		
		dishNo=-1;
		arrDish = [[APPSESSION dishdata] copy];
		for (int i=0; i<[arrDish count]; i++) {
			Product *product = (Product *)[arrDish objectAtIndex:i];
			if ([product.pProductId isEqualToString:dishid]) {
				dishNo=i;
				curProduct = product;
				[self DisplayDishContrl];
				[self DisplayRelateDish];
				break;
			}
		}
		DebugLog(@"+++++++++++++++++++++++++++++++++++++++++++");
		[self showDish];
		
	
		

	}
	return self;
}

#pragma mark 显示菜肴
-(void)showDish
{
	UIView *view = [self viewWithTag:kTagOfDetailView];
	if (view) {
		[view removeFromSuperview];
	}
	view = [self viewWithTag:kTagOfVideoView];
	if (view) {
		[view removeFromSuperview];
	}
	[self clearOperView];
	[controlView StopSound];
	
	if (curProduct)
	{
		
		XRmenuError* error =nil;
		UIImage *photoImg = [UIImage imageWithContentsOfFile:[XRmenuPublic getDishPhotoImageFileName:[NSString stringWithFormat:@"%@",curProduct.pEntityId] Error:&error ]];
		UIImageView *bigImageView = (UIImageView *)[self viewWithTag:kTagOfBigPhoto];
		[bigImageView setImage:photoImg];

		[self refreshControlStatus];
		[self refreshRelateStatus];
		
		error=nil;
		[XRmenuPublic haveAudioFile:curProduct Error:&error];
		if (!error) {
			[controlView PlaySound];
		}
	}
	
}


#pragma mark 手势操作
- (void)handleSwipeLeft:(UISwipeGestureRecognizer *)recognizer {

	if (dishNo==([arrDish count]-1)) {
		//XRmenualert(@"引力网络引力网络引力网络引力网络引力网络引力网络引力网络引力网络引力网络",locimg(@"View/Photo View/theLastPage.png"));
		[self DisplayTerimalView:YES isTheFinished:YES];
		return;
	}
	[self DisplayTerimalView:NO isTheFinished:YES];
	dishNo++;
	curProduct= [arrDish objectAtIndex:dishNo];
	[self showDish];
	
	
	CATransition *animation = [CATransition animation];
    //animation.delegate = self;
    animation.duration = 0.5f;
    animation.timingFunction = UIViewAnimationCurveEaseInOut;
	animation.fillMode = kCAFillModeForwards;
	animation.type = kCATransitionPush;
	animation.subtype = kCATransitionFromRight;  ///kCATransitionFromTop;
	[self.layer addAnimation:animation forKey:@"animation"];	
	[APPSESSION playSoundOnEvent:[XRmenuPublic getGeneralSkinImg:@"View/A_Flips.mp3"]];
	
}

- (void)handleSwipeRight:(UISwipeGestureRecognizer *)recognizer {
	
	if (dishNo<=0 ) {
		//XRmenualert(@"引力网络引力网络引力网络引力网络引力网络引力网络引力网络引力网络引力网络",locimg(@"View/Photo View/theFirstPage.png"));
		[self DisplayTerimalView:YES isTheFinished:NO];
		return;
	}
	dishNo--;
	[self DisplayTerimalView:NO isTheFinished:NO];
	
	//return;
	curProduct= [arrDish objectAtIndex:dishNo];
	[self showDish];

	CATransition *animation = [CATransition animation];
    //animation.delegate = self;
    animation.duration = 0.5f;
    animation.timingFunction = UIViewAnimationCurveEaseInOut;
	animation.fillMode = kCAFillModeForwards;
	animation.type = kCATransitionPush;
	animation.subtype = kCATransitionFromLeft;  ///kCATransitionFromTop;
	[self.layer addAnimation:animation forKey:@"animation"];	

	[APPSESSION playSoundOnEvent:[XRmenuPublic getGeneralSkinImg:@"View/A_Flips.mp3"]];
	
	
}

- (void)handlePinchForm:(UIPinchGestureRecognizer *)recognizer
{
	//DebugLog(@"请重载，scale: %.1f, velocity: %.1f", recognizer.scale, recognizer.velocity);
 
	if (recognizer.state == UIGestureRecognizerStateEnded){
		if (recognizer.scale <1.0) {
			[self switchToAlbum];
		
			return;
		}       
    }  
	
}


- (void)handleSwipeTop:(UISwipeGestureRecognizer *)recognizer
{
	
	if (dishNo==([arrDish count]-1)) {
		//XRmenualert(@"引力网络引力网络引力网络引力网络引力网络引力网络引力网络引力网络引力网络",locimg(@"View/Photo View/theLastPage.png"));
		[self DisplayTerimalView:YES isTheFinished:YES];
		return;
	}
	[self DisplayTerimalView:NO isTheFinished:YES];
	dishNo++;
	curProduct= [arrDish objectAtIndex:dishNo];
	[self showDish];
	
	
	[UIView beginAnimations:@"animationImg" context:nil];
	[UIView setAnimationDuration:0.8f];
	[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
	[UIView setAnimationRepeatAutoreverses:NO];
	
	[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self cache:YES];
	
	[UIView commitAnimations];
	
	[APPSESSION playSoundOnEvent:[XRmenuPublic getGeneralSkinImg:@"View/A_Flips.mp3"]];
	
		
}
-(void)handleSwipeDown:(UISwipeGestureRecognizer *)recognizer
{
	if (dishNo<=0 ) {
		//XRmenualert(@"引力网络引力网络引力网络引力网络引力网络引力网络引力网络引力网络引力网络",locimg(@"View/Photo View/theFirstPage.png"));
		[self DisplayTerimalView:YES isTheFinished:NO];
		return;
	}
	dishNo--;
	[self DisplayTerimalView:NO isTheFinished:NO];
	
	//return;
	curProduct= [arrDish objectAtIndex:dishNo];
	[self showDish];
	
	[UIView beginAnimations:@"animationImg" context:nil];
	[UIView setAnimationDuration:0.8f];
	[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
	[UIView setAnimationRepeatAutoreverses:NO];
	
	[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown forView:self cache:YES];
	
	[UIView commitAnimations];
	
	[APPSESSION playSoundOnEvent:[XRmenuPublic getGeneralSkinImg:@"View/A_Flips.mp3"]];
	
	
}



-(void)handleTapFrom:(UITapGestureRecognizer *)recognizer 
{
	DebugLog(@"开启工具栏");
	postN(kMessage_TopBar_The3rd_Controller);
}



#pragma mark Control委托操作方法

-(void)DisplayVideo
{
	UIView *view = [self viewWithTag:kTagOfVideoView];
	if (!view) {
		DishVideoView *videoView = [[DishVideoView alloc]  initWithViewName:@"Dish Video View" product:curProduct];
		videoView.tag=kTagOfVideoView;
		[self addSubview:videoView];
		[videoView release];		
	}
}

-(void)DisplayDetail
{
		
	
	UIView *view = [self viewWithTag:kTagOfDetailView];
	if (!view) {
		DishDetailView *detailView = [[DishDetailView alloc] initWithStyle:[APPSESSION uistyle] DishSubViewName:@"Dish Detail View" Product:[arrDish objectAtIndex:dishNo] ];
		detailView.tag=kTagOfDetailView;
		detailView.deleagate=self;
		[self addSubview:detailView];
		[detailView release];
	}
}

-(BOOL)AddDish
{
	
	NSInteger dishNum = [XRmenuPublic getProductNum:curProduct];
	if (dishNum>0) {
		[self doDishLayer];
	}
	else {
		[APPSESSION playSoundOnEvent:[XRmenuPublic getGeneralSkinImg:@"View/A_ButtonClicked.mp3"]];
		[XRmenuPublic insertNewOrder:curProduct];
		[self refreshControlStatus];	
		postNWithObj(kMessage_Album_orderedChange, curProduct);
		postN(kMessage_TopBar_orderedChange);
	}


	return YES;
}

-(void)doDishLayer
{
	[self clearOperView];
	NSDictionary* dic =[APPSESSION.allconfig findAtPath:@"UI Skin/default skin/Dish Main View/Dish Album View/DishOperView"];
	DishOperView* operView =[[DishOperView alloc] initWithConfig:dic type:DishOper_Photo item:curProduct];
	
	operView.delegate=self;
	operView.center =  CGPointMake(950,
								   630);
	operView.tag= kTagOperView;
	[self addSubview:operView]; 
	[operView release];
}

#pragma mark 操作层的委托方法
-(void)dishOperView:(DishOperView *)operView didTappedWith:(DishOperViewOperType)aType
{ 
	BOOL doSucess=NO;
	switch (aType) {
		case DishOperView_Detail:
		{
			//小图
			[self switchToAlbum];
			return;
			break;
		}
		case DishOperView_close:
			
			break;
		case DishOperView_Plus:
			//加1
			doSucess = [XRmenuPublic updateProductNum:curProduct Num:num(1)];
			break;
		case DishOperView_Miuns:
			//减1
			doSucess = [XRmenuPublic updateProductNum:curProduct Num:num(-1)];
			if ([XRmenuPublic getProductNum:curProduct]==0) {
				[self clearOperView];
			}
			break;
		case DishOperView_Delete:
			doSucess = [XRmenuPublic clearProduct:curProduct];
			if ([XRmenuPublic getProductNum:curProduct]==0) {
				[self clearOperView];
			}
			break;
		default:
			break;
	} 
	

	
	if (doSucess) {
		
	}
	//lou-数据库返回doSucess有时不正确
	[self refreshControlStatus];
	postNWithObj(kMessage_Album_orderedChange, curProduct);
	postN(kMessage_TopBar_orderedChange);

//	[self playAnimationWithThumb:self.currentDishThumbView type:aType];
}



-(void)switchToAlbum
{
	[controlView StopSound];
	Product *product = (Product *)[arrDish objectAtIndex:dishNo];
	[APPSESSION playSoundOnEvent:[XRmenuPublic getGeneralSkinImg:@"View/A_Order.mp3"]];
	[[NSNotificationCenter defaultCenter] postNotificationName:kMessage_Dish_PhotoToAlbum object:product.pProductId userInfo:nil];
	
}

-(void)clearOperView
{
	UIView *view = [self viewWithTag:kTagOperView];
	if (view) {
		[view removeFromSuperview];
	}
}


#pragma mark 操作方法
-(void)DisplayTerimalView:(BOOL)isShowTerimal isTheFinished:(BOOL)isTheFinished
{
	UIView *view = [self viewWithTag:kTagOfTerimalView];
	if (view) {
		[view removeFromSuperview];
	}
		
	if (isShowTerimal) {
			DishTermialView *teriamlView = [[DishTermialView alloc] initWithStyle:@"Dish Termial View" isTheFinished:isTheFinished ];
			teriamlView.tag=kTagOfTerimalView;
			[self addSubview:teriamlView];
			[teriamlView release];
	}
}

-(void)DisplayRelateDish
{
	relateView = [[DishRelateView alloc] initWithViewName:@"Dish Relate View"]; 
	relateView.tag=kTagOfRelateView;
	[self addSubview:relateView];


}

-(void)refreshRelateStatus
{
	if ([APPSESSION relateDishLayerStatus]==0) {
		[relateView setHidden:YES];
	}
	else {
		NSArray *arrRelate = [XRmenuPublic haveRelationProduct:curProduct];
		if ([arrRelate count]>0) {
			[relateView showRelateDish:arrRelate];
			[relateView setHidden:NO];
		}
		else {
			[relateView setHidden:YES];
		}

	}

}

-(void)DisplayDishContrl
{
	controlView = [[DishControl alloc]  initWithViewName:@"Dish Control View"]; 
	controlView.tag=kTagOfDishControl;
	controlView.deleagate=self;
	[self addSubview:controlView];
	
}

-(void)refreshControlStatus
{
	DebugLog(@"刷新菜单");
	[controlView refreshControlStatus:curProduct cProductIndex:dishNo cProductCount:[arrDish count]];
}

-(void)dealloc
{
	removeNObserver(kMessage_Dish_changeRelateDish);
	removeNObserver(kMessage_Photo_orderedChange);
	
	[arrDish release];
	[controlView release];
	[relateView release];
	[super dealloc];
}

#pragma mark 语言切换
-(void) changeLanguage:(NSNotification *)notification
{
	// todo: 切换语言
	NSDictionary *dict = [APPSESSION.allconfig findAtPath:[NSString stringWithFormat:@"UI Skin/%@/Dish Main View/Dish Photo View", APPSESSION.uistyle]];
	if (dict) {
		NSArray *bts = (NSArray*)[dict objectForKey:@"button list"];
		[self translateButtons:bts];
	}
	
}

#pragma mark 相关菜切换
-(void)changeRelateDish:(NSNotification *)notification
{
	DebugLog(@"相关菜相关菜相关菜");
	curProduct = (Product *)notification.object;
	[self showDish];
}


@end
